<template>
  <div>
    <Form inline :label-width="90">
      <Form-item :label="$t('module.userManage.roleId')">
        <Input v-model="roleInfo.id"  disabled />
      </Form-item>
      <Form-item :label="$t('module.userManage.roleName')">
        <Input v-model="roleInfo.name"  disabled />
      </Form-item>
    </Form>
    <p class="assign-user">{{this.$t('module.userManage.assignResource')}}</p>
    <div class="divid-line"></div>
    <div class="transfer-content">
      <!-- 左侧区域 -->
      <div class="transfer-left tansDiv">
        <div class="left-content">
          <InnerPageTable ref="transferLeftListTable" :config="configLeft">
            <Form
              :model="conditionLeft"
              ref="transferLeftListForm"
              slot="search-bar"
              label-position="top"
              class="ivu-form-no-margin-bottom"
              inline
              :label-width="0"
            >
              <Form-item prop="userName">
                <Input
                  icon="ios-search"
                  :placeholder="$t('common.searchN')"
                  @input="searchHandler()"
                  v-model="conditionLeft.resourceName"
                />
              </Form-item>
            </Form>
          </InnerPageTable>
        </div>
      </div>

      <!-- 添加用户、移除用户 中间穿梭按钮 -->
      <div class="transfer-center tansDiv">
        <div>
          <div class="addUserBtn" @click="addSelectUsersBtn">
            {{$t('module.userManage.addResource')}}
            <span>&gt;&gt;</span>
          </div>
          <div class="deleUserBtn" @click="deletSelectUsersBtn">
            <span>&lt;&lt;</span>
            {{$t('module.userManage.delResource')}}
          </div>
        </div>
      </div>

      <!-- 右侧区域 -->
      <div class="transfer-right tansDiv">
        <div class="right-content">
          <InnerPageTable ref="transferRightListTable" :config="configRight"></InnerPageTable>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { unChosedResource, chosedResource, insertResource, deleteResource } from '@api/flowable/userManageAPI.js'
import { unchosedResCol } from './columns.js'
export default {
  name: 'assignResource',
  props: {
    roleInfo: {}
  },
  data() {
    return {
      conditionLeft: {
        resourceName: ''
      },
      configLeft: {
        loadDataFn: ({ responseProcess, param }) => {
          unChosedResource(param).then(({ data }) => {
            data.currentPage = data.pageNum
            delete data.pageNum
            data.recordCount = data.total
            delete data.total
            data.recordList = data.records
            delete data.records
            responseProcess(data)
          })
        },
        columnsFn: unchosedResCol.bind(this),
        autoFirst: true,
        initParam: { roleId: this.roleInfo.id },
        onSelectionChange: this.selectResource
      },
      configRight: {
        loadDataFn: ({ responseProcess, param }) => {
          chosedResource(param).then(({ data }) => {
            data.currentPage = data.pageNum
            delete data.pageNum
            data.recordCount = data.total
            delete data.total
            data.recordList = data.records
            delete data.records
            responseProcess(data)
          })
        },
        columnsFn: unchosedResCol.bind(this),
        autoFirst: true,
        initParam: { roleId: this.roleInfo.id },
        onSelectionChange: this.delResource
      },
      resourceIds: [],
      delResourceIds: []
    }
  },
  methods: {
    //搜索
    searchHandler() {
      this.$refs['transferLeftListTable'].load(this.conditionLeft, 1)
    },
    //选择要添加的资源
    selectResource(data) {
      this.resourceIds = data.map(item => item.id)
    },
    //选择要移除的资源
    delResource(data) {
      this.delResourceIds = data.map(item => item.id)
    },
    //添加资源
    addSelectUsersBtn() {
      const resourceIds = this.resourceIds
      let params = {
        id: this.roleInfo.id,
        resourceIds
      }
      if (resourceIds && resourceIds.length) {
        insertResource(params).then(() => {
          this.resourceIds = []
          this.$Message.success({
            content: this.$t('module.userManage.addSuccess'),
            duration: 3
          })
          this.$refs['transferRightListTable'].load()
          this.$refs['transferLeftListTable'].load()
        })
      } else {
        this.$Message.error({
          content: this.$t('module.userManage.selectAddRes'),
          duration: 3
        })
      }
    },
    //删除资源
    deletSelectUsersBtn() {
      const delResourceIds = this.delResourceIds
      let params = {
        id: this.roleInfo.id,
        resourceIds: delResourceIds
      }
      if (delResourceIds && delResourceIds.length) {
        deleteResource(params).then(() => {
          this.delResourceIds = []
          this.$Message.success({
            content: this.$t('module.userManage.delSuccess'),
            duration: 3
          })
          this.$refs['transferRightListTable'].load()
          this.$refs['transferLeftListTable'].load()
        })
      } else {
        this.$Message.error({
          content: this.$t('module.userManage.selectDelRes'),
          duration: 3
        })
      }
    }
  }
}
</script>
<style lang="less">
.assign-user {
  padding: 10px 5px;
  font-size: 14px;
  font-weight: 900;
}
.divid-line {
  margin: 0 -16px;
  border-bottom: 1px solid rgb(224, 221, 221);
}
.transfer-content {
  width: 100%;
  padding: 0 10px;
}
.transfer-content {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  margin-top: 16px;
  .tansDiv {
    .single-table-con {
      padding: 0;
    }
    .run-search-bar div {
      margin: 0;
      width: 100%;
    }
    .ivu-form-inline {
      padding: 0;
      min-height: 0;
    }
    .ivu-form-inline .ivu-form-item {
      margin: 0;
    }
    .ivu-form-inline .ivu-input-wrapper {
      width: 100%;
    }
    .ivu-input {
      border-radius: 3px;
    }
    .inner-table {
      min-height: 400px;
    }
    .self-adaption-wrap {
      margin-top: 10px;
    }
    .ivu-table {
      font-size: 12px;
    }
    .table-bar {
      margin: 0 0 10px;
    }
  }
  .transfer-left {
    width: 45%;
    display: flex;
    justify-content: flex-start;
    .left-content {
      width: 100%;
      border: 1px solid rgb(224, 221, 221);
      border-radius: 5px;
      padding: 10px;
    }
  }
  .transfer-center {
    width: 10%;
    position: relative;
    & > div {
      width: 80%;
      position: absolute;
      top: 45%;
      left: 10%;
    }
    .addUserBtn {
      margin-bottom: 20px;
      background: #ff6f4b;
      color: white;
    }
    .deleUserBtn {
      color: #ff6f4b;
      border: 1px solid #ff6f4b;
    }
    .addUserBtn,
    .deleUserBtn {
      min-height: 25px;
      line-height: 25px;
      padding: 0 5px;
      text-align: center;
      cursor: pointer;
      user-select: none;
    }
  }
  .transfer-right {
    width: 45%;
    display: flex;
    justify-content: flex-end;
    .right-content {
      width: 100%;
      border: 1px solid rgb(224, 221, 221);
      border-radius: 5px;
      padding: 10px;
    }
  }
}
</style>